<template>
    <div class="chart-test">
        <h1>图表显示测试页面</h1>

        <el-row :gutter="20">
            <el-col :span="12">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>消费频次分析测试</span>
                        </div>
                    </template>
                    <div ref="testChart1" class="chart-container" style="height: 400px; border: 2px solid #f0f0f0;">
                    </div>
                </el-card>
            </el-col>

            <el-col :span="12">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>年龄分布测试</span>
                        </div>
                    </template>
                    <div ref="testChart2" class="chart-container" style="height: 400px; border: 2px solid #f0f0f0;">
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="12">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>资源监控测试</span>
                        </div>
                    </template>
                    <div ref="testChart3" class="chart-container" style="height: 400px; border: 2px solid #f0f0f0;">
                    </div>
                </el-card>
            </el-col>

            <el-col :span="12">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>预警统计测试</span>
                        </div>
                    </template>
                    <div ref="testChart3" class="chart-container" style="height: 400px; border: 2px solid #f0f0f0;">
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <div style="margin-top: 20px;">
            <el-button type="primary" @click="testAllCharts">测试所有图表</el-button>
            <el-button type="success" @click="refreshCharts">刷新图表</el-button>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'

// 图表引用
const testChart1 = ref(null)
const testChart2 = ref(null)
const testChart3 = ref(null)
const testChart4 = ref(null)

// 测试消费频次图表
const testConsumptionChart = () => {
    if (!testChart1.value) {
        console.error('测试图表1容器未找到')
        return
    }

    console.log('测试图表1容器:', testChart1.value)
    console.log('测试图表1尺寸:', testChart1.value.offsetWidth, 'x', testChart1.value.offsetHeight)

    const chart = echarts.init(testChart1.value)
    const option = {
        title: { text: '消费频次测试', left: 'center' },
        series: [{
            type: 'pie',
            radius: '50%',
            data: [
                { value: 335, name: '高频客户' },
                { value: 310, name: '中频客户' },
                { value: 234, name: '低频客户' }
            ]
        }]
    }

    chart.setOption(option)
    console.log('测试图表1已设置选项')
}

// 测试年龄分布图表
const testAgeChart = () => {
    if (!testChart2.value) {
        console.error('测试图表2容器未找到')
        return
    }

    console.log('测试图表2容器:', testChart2.value)
    console.log('测试图表2尺寸:', testChart2.value.offsetWidth, 'x', testChart2.value.offsetHeight)

    const chart = echarts.init(testChart2.value)
    const option = {
        title: { text: '年龄分布测试', left: 'center' },
        xAxis: { data: ['18-25', '26-35', '36-45'] },
        yAxis: {},
        series: [{
            type: 'bar',
            data: [120, 200, 150]
        }]
    }

    chart.setOption(option)
    console.log('测试图表2已设置选项')
}

// 测试所有图表
const testAllCharts = () => {
    console.log('开始测试所有图表...')
    testConsumptionChart()
    testAgeChart()
    ElMessage.success('图表测试完成，请查看控制台输出')
}

// 刷新图表
const refreshCharts = () => {
    console.log('刷新所有图表...')
    testAllCharts()
    ElMessage.success('图表已刷新')
}

onMounted(() => {
    nextTick(() => {
        console.log('组件已挂载，开始初始化测试图表...')
        setTimeout(() => {
            testAllCharts()
        }, 500)
    })
})
</script>

<style scoped>
.chart-test {
    padding: 20px;
}

.chart-container {
    width: 100%;
    min-height: 400px;
    background: #fafafa;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>